package com.aramis.mydytt.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.children
import com.aramis.mydytt.R
import com.aramis.mydytt.ext.KV_HISTORY_KEY
import com.aramis.mydytt.ext.dip
import com.aramis.mydytt.ext.screenWidth
import com.aramis.mydytt.util.MMKVUtil

/**
 * Description: SearchHistoryView
 * @author lizhidan
 * Created: 2021/5/25
 */
class SearchHistoryView : LinearLayout {
    private val marginDivider = dip(10)
    private val paddingDivider = dip(8)
    private val itemMaxWidth = dip(160)
    private val itemHeight = dip(30)
    private var totalWidth = 0
    var onItemClickListener: ((String) -> Unit)? = null

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        orientation = VERTICAL
        inflate(context, R.layout.view_search_history, this)
        totalWidth = context.screenWidth() - dip(10) - dip(10)

        findViewById<View>(R.id.image_history_delete).setOnClickListener {
            this.children.iterator().forEach { view ->
                if (view is LinearLayout) {
                    view.removeAllViews()
                }
            }
            MMKVUtil.putString(KV_HISTORY_KEY, null)
        }
    }

    fun setHistory(history: List<String>) {
        var loopIndex = 0
        while (loopIndex != -1 && loopIndex < history.size) {
            loopIndex = addSingleLine(loopIndex, history)
        }
    }

    private fun addSingleLine(start: Int, history: List<String>): Int {
        val innerLayout = LinearLayout(context)
        val innerLayoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        innerLayoutParams.setMargins(0, dip(5), 0, 0)
        innerLayout.layoutParams = innerLayoutParams
        var itemWidthTotal = 0
        var skipMarginStart = true
        (start until history.size).forEach { index ->
            val textView = TextView(context)
            textView.text = history[index]
            textView.gravity = Gravity.CENTER
            textView.setTextColor(Color.WHITE)
            textView.maxWidth = itemMaxWidth
            textView.isClickable = true
            textView.setLines(1)
            textView.setBackgroundResource(R.drawable.bg_search_history_item)
            textView.setPadding(paddingDivider, 0, paddingDivider, 0)
            val textLayoutParams = MarginLayoutParams(LayoutParams.WRAP_CONTENT, itemHeight)
            textLayoutParams.setMargins(
                if (skipMarginStart) 0 else marginDivider, 0, 0, 0
            )
            skipMarginStart = false
            textView.layoutParams = textLayoutParams
            textView.setOnClickListener {
                onItemClickListener?.invoke(history[index])
            }
            innerLayout.addView(textView)
            val length = textView.textSize.toInt() * history[index].length + marginDivider + paddingDivider * 2
            itemWidthTotal += if (length > itemMaxWidth) itemMaxWidth else length
            if (itemWidthTotal > totalWidth) {
                innerLayout.removeView(textView)
                this.addView(innerLayout)
                return index
            }
        }
        this.addView(innerLayout)
        return -1
    }
}